﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using SHack.Helper;
using System.Windows.Forms;

namespace SHack
{
    class Config
    {
        private static SortedList<string, Control> _slControl = new SortedList<string, Control>();
        private static SortedList<string, string> _slConfig = new SortedList<string, string>();
        private static string _filePath;
        public static string URLFile;


        public static int GetInt(string key, int def = 0)
        { 
            return Convert.ToInt32(Get(key,def.ToString()));
        }

        public static bool GetBool(string key, bool def = false)
        {
            return Get(key, def.ToString()).Equals("True");
        }

        public static string Get(string key,string def="")
        {
            if (_slConfig.ContainsKey(key))
                return _slConfig[key];

            return def;
        }

        public static void Get(Control obj,string def="")
        {
            string val = Get(obj.Name, def);

            if (obj is TextBox || obj is ComboBox)
            {
                obj.Text = val;

            }
            else if (obj is CheckBox)
            {
                CheckBox chk = (CheckBox)obj;
                chk.Checked = val.Equals("True");
            }
            else if (obj is RadioButton)
            {
                RadioButton rd = (RadioButton)obj;
                rd.Checked = val.Equals("True");
            }
            _slControl[obj.Name] = obj;
            
        }

        public static void Set(string key, string value)
        {
            _slConfig[key] = value;
        }

        public static void Load(string filePath)
        {
            _filePath = filePath;
            object obj = DataToFileHelper.Load(filePath);
            if (obj != null)
            {
                _slConfig = (SortedList<string, string>)obj;
            }

        }

        public static void Save()
        {
            foreach (Control obj in _slControl.Values)
            {
                if (obj is TextBox || obj is ComboBox)
                {
                    _slConfig[obj.Name] = obj.Text;

                }
                else if (obj is CheckBox)
                {
                    CheckBox chk = (CheckBox)obj;
                    _slConfig[obj.Name]=chk.Checked.ToString();
                }
                else if (obj is RadioButton)
                {
                    RadioButton rd = (RadioButton)obj;
                    _slConfig[obj.Name] = rd.Checked.ToString();
                }

            }


            DataToFileHelper.Save(_filePath, _slConfig);        
        }
    }
}
